/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Alexandre
 */
@Entity
@Table(name = "groupDefinition")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "GroupDefinition.findAll", query = "SELECT g FROM GroupDefinition g"),
    @NamedQuery(name = "GroupDefinition.findByIdgroup", query = "SELECT g FROM GroupDefinition g WHERE g.idgroup = :idgroup"),
    @NamedQuery(name = "GroupDefinition.findByName", query = "SELECT g FROM GroupDefinition g WHERE g.name = :name"),
    @NamedQuery(name = "GroupDefinition.findByDescription", query = "SELECT g FROM GroupDefinition g WHERE g.description = :description"),
    @NamedQuery(name = "GroupDefinition.findByPassword", query = "SELECT g FROM GroupDefinition g WHERE g.password = :password")})
public class GroupDefinition implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idgroup")
    private Integer idgroup;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "name")
    private String name;
    @Size(max = 256)
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 256)
    @Column(name = "password")
    private String password;
    @JoinTable(name = "user_group", joinColumns = {@JoinColumn(name = "groupId", referencedColumnName = "idgroup")
        }, inverseJoinColumns = {
        @JoinColumn(name = "user", referencedColumnName = "username")})
    @ManyToMany
    private Collection<User> userCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "groupId")
    private Collection<Recepit> recepitCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "groupId")
    private Collection<Payment> paymentCollection;
    @JoinColumn(name = "creator", referencedColumnName = "username")
    @ManyToOne(optional = false)
    private User creator;

    public GroupDefinition() {
        userCollection = new ArrayList<User>();
        recepitCollection = new ArrayList<Recepit>();
        paymentCollection = new ArrayList<Payment>();
    }

    public GroupDefinition(Integer idgroup) {
        this.idgroup = idgroup;
    }

    public GroupDefinition(Integer idgroup, String name, String password) {
        this.idgroup = idgroup;
        this.name = name;
        this.password = password;
    }

    public Integer getIdgroup() {
        return idgroup;
    }

    public void setIdgroup(Integer idgroup) {
        this.idgroup = idgroup;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @XmlTransient
    public Collection<User> getUserCollection() {
        return userCollection;
    }

    public void setUserCollection(Collection<User> userCollection) {
        this.userCollection = userCollection;
    }

    @XmlTransient
    public Collection<Recepit> getRecepitCollection() {
        return recepitCollection;
    }

    public void setRecepitCollection(Collection<Recepit> recepitCollection) {
        this.recepitCollection = recepitCollection;
    }

    @XmlTransient
    public Collection<Payment> getPaymentCollection() {
        return paymentCollection;
    }

    public void setPaymentCollection(Collection<Payment> paymentCollection) {
        this.paymentCollection = paymentCollection;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idgroup != null ? idgroup.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GroupDefinition)) {
            return false;
        }
        GroupDefinition other = (GroupDefinition) object;
        if ((this.idgroup == null && other.idgroup != null) || (this.idgroup != null && !this.idgroup.equals(other.idgroup))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.GroupDefinition[ idgroup=" + idgroup + " ]";
    }

}
